import Taro from "@tarojs/taro";

export const setStorage = (key: any, value: any) => {
  return Taro.setStorageSync(key, value)
}

export const getStorage = (key: any) => {
  return Taro.getStorageSync(key)
}

export const clearStorage = (key: any) => {
  return Taro.removeStorageSync(key)
}

export const getCurrentPageUrlWithArgs = () => {
  //获取带参链接
  const pages = Taro.getCurrentPages()
  const currentPage = pages[pages.length - 1]
  const url = currentPage.route
  const options = currentPage.options
  let urlWithArgs = `/${url}?`
  for (let key in options) {
    const value = options[key]
    urlWithArgs += `${key}=${value}&`
  }
  urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1);
  return urlWithArgs;
}

//日期字符串转成时间戳
//例如var date = '2015-03-05 17:59:00.0';
export const dateStrChangeTimeTamp = (dateStr) => {
  dateStr = dateStr.substring(0, 19);
  dateStr = dateStr.replace(/-/g, '/');
  return new Date(dateStr).getTime();
}

//时间戳转为日期，年月日
export const timestampToTimeYMD = (timestamp) => {
  const date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  const Y = date.getFullYear() + '年';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月';
  const D = date.getDate() + '日';
  return Y + M + D;
}
// 对数组进行分类
export const arrayGroupBy = (objectArray, property) => {
return objectArray.reduce(function (acc, obj) {
    let key = obj[property]
    if (!acc[key]) {
      acc[key] = []
    }
    acc[key].push(obj)
    return acc
  }, {})
}